<?php

class UserForm extends TPage
{
	const AR = "UserRecord";
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " Administrator | Add/Update User";
		
		if (!$this->IsPostBack)
		{
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = "user_type_id > 0";
			$criteria->OrdersBy["user_type_name"] = "asc";
			$this->cboStatusSelector->DataSource = TPropertyValue::ensureArray($this->Application->Parameters["USER_STATUS"]);
			$this->cboStatusSelector->DataBind();
			$activeRecord = $this->getItem();
			if ($activeRecord && $activeRecord->ID > 0)
			{
				// Populates the input controls with the existing post data
				$this->lblHeader->Text = "Update user: ".$activeRecord->Email;
				$this->txtEmail->Text = $activeRecord->Email;
				$this->txtPassword->Text = $activeRecord->Password;
				$this->txtFirstName->Text = $activeRecord->FirstName;
				$this->txtLastName->Text = $activeRecord->LastName;
				$this->txtAddress->Text = $activeRecord->Address;
				$this->txtCity->Text = $activeRecord->City;
				$this->txtPostalCode->Text = $activeRecord->PostalCode;
				$this->txtEmail->Text = $activeRecord->Email;
				$this->txtPhone->Text = $activeRecord->Phone;
				$this->txtPhone2->Text = $activeRecord->Phone2;				
				$this->ddlCountry->SelectedValue = $activeRecord->Country;
				$this->cboTypeSelector->SelectedValue = $activeRecord->UserType;
				$this->cboStatusSelector->SelectedValue = $activeRecord->Status;
			}
			else
			{
				$this->lblHeader->Text = "Add new user";
				$this->cboStatusSelector->SelectedValue = 1;
			}
		}
	}

	protected function getItem()
	{
		if ($this->Request->Contains("id") && $this->Request->Contains("alias"))
		{
			// use Active Record to look for the specified post ID
			$activeRecord = Prado::createComponent(self::AR)->finder()->findByuser_idAnduser_email(TPropertyValue::ensureInteger($this->Request['id']), $this->Request['alias']);
			if($activeRecord === null)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","user");
				$this->mainBox->Visible = false;
			}
			return $activeRecord;
		}
		else
		{
			return Prado::createComponent(self::AR);
		}
	}

	private function bindItem()
	{
		$activeRecord = $this->getItem();
		$activeRecord->Email = $activeRecord->Username = $this->txtEmail->SafeText;
		$activeRecord->Password = ($activeRecord->Password != $this->txtPassword->SafeText) ? md5($this->txtPassword->SafeText) : $activeRecord->Password;
		$activeRecord->FirstName = $this->txtFirstName->SafeText;
		$activeRecord->LastName = $this->txtLastName->SafeText;
		$activeRecord->Phone = $this->txtPhone->SafeText;
		$activeRecord->Phone2 = $this->txtPhone2->SafeText;
		$activeRecord->Address = $this->txtAddress->SafeText;
		$activeRecord->City = $this->txtCity->SafeText;
		$activeRecord->Country = $this->ddlCountry->SelectedValue;
		$activeRecord->PostalCode = $this->txtPostalCode->SafeText;				
		$activeRecord->UserType = $this->cboTypeSelector->SelectedValue;
		$activeRecord->Status = $this->cboStatusSelector->SelectedValue;

		return $activeRecord;
	}

	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			$activeRecord = $this->bindItem();
			try
			{
				$action = ($activeRecord->ID>0 ? "update-success" : "add-success");
				$msg = $this->Application->getModule("message")->translate(($activeRecord->ID>0 ? "UPDATE_SUCCESS" : "ADD_SUCCESS"),"User",$activeRecord->Email);
				$activeRecord->save();
				if (strlen($this->Request["refUrl"])>0)
					$url = urldecode($this->Request["refUrl"])."&action=$action&msg=$msg";
				else  $url = $this->Service->ConstructUrl("admin.ManageUsers",array("action"=>$action, "msg"=>$msg));
				$this->Response->redirect($url);
			}
			catch(TException $e)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate(($activeRecord->ID>0 ? "UPDATE_FAILED" : "ADD_FAILED"),"User",$activeRecord->Email);
			}
		}
	}

	protected function uniqueCheck_ServerValidated($sender, $param)
	{
		if ($param->Value != '')
		{
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = "user_email = :name";
			$criteria->Parameters[":name"] = $param->Value;
			$activeRecord = $this->getItem();
			if ($activeRecord && $activeRecord->ID > 0) $criteria->Condition .= " and user_id <> '".$activeRecord->ID."'";
			$param->IsValid = count(Prado::createComponent(self::AR)->finder()->find($criteria)) == 0;
		}
	}
}

?>